package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.AvueServer4jApp;
import com.xjgzinfo.usdp.config.TestSecurityConfiguration;
import com.xjgzinfo.usdp.domain.AppFile;
import com.xjgzinfo.usdp.repository.AppFileRepository;
import com.xjgzinfo.usdp.service.AppFileQueryService;
import com.xjgzinfo.usdp.service.AppFileService;
import com.xjgzinfo.usdp.service.dto.AppFileDTO;
import com.xjgzinfo.usdp.service.mapper.AppFileMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link AppFileResource} REST controller.
 */
@SpringBootTest(classes = {AvueServer4jApp.class, TestSecurityConfiguration.class})
@AutoConfigureMockMvc
@WithMockUser
public class AppFileResourceIT {

    private static final Long DEFAULT_FILE_ID = 1L;
    private static final Long UPDATED_FILE_ID = 2L;
    private static final Long SMALLER_FILE_ID = 1L - 1L;

    private static final String DEFAULT_FILE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_FILE_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_FILE_URI = "AAAAAAAAAA";
    private static final String UPDATED_FILE_URI = "BBBBBBBBBB";

    private static final String DEFAULT_ORIGINAL_FILE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_ORIGINAL_FILE_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_FILE_PATH = "AAAAAAAAAA";
    private static final String UPDATED_FILE_PATH = "BBBBBBBBBB";

    private static final Long DEFAULT_USER_ID = 1L;
    private static final Long UPDATED_USER_ID = 2L;
    private static final Long SMALLER_USER_ID = 1L - 1L;

    @Autowired
    private AppFileRepository appFileRepository;

    @Autowired
    private AppFileMapper appFileMapper;

    @Autowired
    private AppFileService appFileService;

    @Autowired
    private AppFileQueryService appFileQueryService;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restAppFileMockMvc;

    private AppFile appFile;

    /**
     * Create an entity for this test.
     * <p>
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static AppFile createEntity(EntityManager em) {
        AppFile appFile = new AppFile()
                .fileId(DEFAULT_FILE_ID)
                .fileName(DEFAULT_FILE_NAME)
                .fileUri(DEFAULT_FILE_URI)
                .originalFileName(DEFAULT_ORIGINAL_FILE_NAME)
                .filePath(DEFAULT_FILE_PATH)
                .userId(DEFAULT_USER_ID);
        return appFile;
    }

    /**
     * Create an updated entity for this test.
     * <p>
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static AppFile createUpdatedEntity(EntityManager em) {
        AppFile appFile = new AppFile()
                .fileId(UPDATED_FILE_ID)
                .fileName(UPDATED_FILE_NAME)
                .fileUri(UPDATED_FILE_URI)
                .originalFileName(UPDATED_ORIGINAL_FILE_NAME)
                .filePath(UPDATED_FILE_PATH)
                .userId(UPDATED_USER_ID);
        return appFile;
    }

    @BeforeEach
    public void initTest() {
        appFile = createEntity(em);
    }

    @Test
    @Transactional
    public void createAppFile() throws Exception {
        int databaseSizeBeforeCreate = appFileRepository.findAll().size();
        // Create the AppFile
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);
        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isCreated());

        // Validate the AppFile in the database
        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeCreate + 1);
        AppFile testAppFile = appFileList.get(appFileList.size() - 1);
        assertThat(testAppFile.getFileId()).isEqualTo(DEFAULT_FILE_ID);
        assertThat(testAppFile.getFileName()).isEqualTo(DEFAULT_FILE_NAME);
        assertThat(testAppFile.getFileUri()).isEqualTo(DEFAULT_FILE_URI);
        assertThat(testAppFile.getOriginalFileName()).isEqualTo(DEFAULT_ORIGINAL_FILE_NAME);
        assertThat(testAppFile.getFilePath()).isEqualTo(DEFAULT_FILE_PATH);
        assertThat(testAppFile.getUserId()).isEqualTo(DEFAULT_USER_ID);
    }

    @Test
    @Transactional
    public void createAppFileWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = appFileRepository.findAll().size();

        // Create the AppFile with an existing ID
        appFile.setId(1L);
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);

        // An entity with an existing ID cannot be created, so this API call must fail
        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        // Validate the AppFile in the database
        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void checkFileIdIsRequired() throws Exception {
        int databaseSizeBeforeTest = appFileRepository.findAll().size();
        // set the field null
        appFile.setFileId(null);

        // Create the AppFile, which fails.
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);


        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkFileNameIsRequired() throws Exception {
        int databaseSizeBeforeTest = appFileRepository.findAll().size();
        // set the field null
        appFile.setFileName(null);

        // Create the AppFile, which fails.
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);


        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkFileUriIsRequired() throws Exception {
        int databaseSizeBeforeTest = appFileRepository.findAll().size();
        // set the field null
        appFile.setFileUri(null);

        // Create the AppFile, which fails.
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);


        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkOriginalFileNameIsRequired() throws Exception {
        int databaseSizeBeforeTest = appFileRepository.findAll().size();
        // set the field null
        appFile.setOriginalFileName(null);

        // Create the AppFile, which fails.
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);


        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkFilePathIsRequired() throws Exception {
        int databaseSizeBeforeTest = appFileRepository.findAll().size();
        // set the field null
        appFile.setFilePath(null);

        // Create the AppFile, which fails.
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);


        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkUserIdIsRequired() throws Exception {
        int databaseSizeBeforeTest = appFileRepository.findAll().size();
        // set the field null
        appFile.setUserId(null);

        // Create the AppFile, which fails.
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);


        restAppFileMockMvc.perform(post("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void getAllAppFiles() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList
        restAppFileMockMvc.perform(get("/api/app-files?sort=id,desc"))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.[*].id").value(hasItem(appFile.getId().intValue())))
                .andExpect(jsonPath("$.[*].fileId").value(hasItem(DEFAULT_FILE_ID.intValue())))
                .andExpect(jsonPath("$.[*].fileName").value(hasItem(DEFAULT_FILE_NAME)))
                .andExpect(jsonPath("$.[*].fileUri").value(hasItem(DEFAULT_FILE_URI)))
                .andExpect(jsonPath("$.[*].originalFileName").value(hasItem(DEFAULT_ORIGINAL_FILE_NAME)))
                .andExpect(jsonPath("$.[*].filePath").value(hasItem(DEFAULT_FILE_PATH)))
                .andExpect(jsonPath("$.[*].userId").value(hasItem(DEFAULT_USER_ID.intValue())));
    }

    @Test
    @Transactional
    public void getAppFile() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get the appFile
        restAppFileMockMvc.perform(get("/api/app-files/{id}", appFile.getId()))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.id").value(appFile.getId().intValue()))
                .andExpect(jsonPath("$.fileId").value(DEFAULT_FILE_ID.intValue()))
                .andExpect(jsonPath("$.fileName").value(DEFAULT_FILE_NAME))
                .andExpect(jsonPath("$.fileUri").value(DEFAULT_FILE_URI))
                .andExpect(jsonPath("$.originalFileName").value(DEFAULT_ORIGINAL_FILE_NAME))
                .andExpect(jsonPath("$.filePath").value(DEFAULT_FILE_PATH))
                .andExpect(jsonPath("$.userId").value(DEFAULT_USER_ID.intValue()));
    }


    @Test
    @Transactional
    public void getAppFilesByIdFiltering() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        Long id = appFile.getId();

        defaultAppFileShouldBeFound("id.equals=" + id);
        defaultAppFileShouldNotBeFound("id.notEquals=" + id);

        defaultAppFileShouldBeFound("id.greaterThanOrEqual=" + id);
        defaultAppFileShouldNotBeFound("id.greaterThan=" + id);

        defaultAppFileShouldBeFound("id.lessThanOrEqual=" + id);
        defaultAppFileShouldNotBeFound("id.lessThan=" + id);
    }


    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId equals to DEFAULT_FILE_ID
        defaultAppFileShouldBeFound("fileId.equals=" + DEFAULT_FILE_ID);

        // Get all the appFileList where fileId equals to UPDATED_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.equals=" + UPDATED_FILE_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsNotEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId not equals to DEFAULT_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.notEquals=" + DEFAULT_FILE_ID);

        // Get all the appFileList where fileId not equals to UPDATED_FILE_ID
        defaultAppFileShouldBeFound("fileId.notEquals=" + UPDATED_FILE_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsInShouldWork() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId in DEFAULT_FILE_ID or UPDATED_FILE_ID
        defaultAppFileShouldBeFound("fileId.in=" + DEFAULT_FILE_ID + "," + UPDATED_FILE_ID);

        // Get all the appFileList where fileId equals to UPDATED_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.in=" + UPDATED_FILE_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsNullOrNotNull() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId is not null
        defaultAppFileShouldBeFound("fileId.specified=true");

        // Get all the appFileList where fileId is null
        defaultAppFileShouldNotBeFound("fileId.specified=false");
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId is greater than or equal to DEFAULT_FILE_ID
        defaultAppFileShouldBeFound("fileId.greaterThanOrEqual=" + DEFAULT_FILE_ID);

        // Get all the appFileList where fileId is greater than or equal to UPDATED_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.greaterThanOrEqual=" + UPDATED_FILE_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId is less than or equal to DEFAULT_FILE_ID
        defaultAppFileShouldBeFound("fileId.lessThanOrEqual=" + DEFAULT_FILE_ID);

        // Get all the appFileList where fileId is less than or equal to SMALLER_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.lessThanOrEqual=" + SMALLER_FILE_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsLessThanSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId is less than DEFAULT_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.lessThan=" + DEFAULT_FILE_ID);

        // Get all the appFileList where fileId is less than UPDATED_FILE_ID
        defaultAppFileShouldBeFound("fileId.lessThan=" + UPDATED_FILE_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileIdIsGreaterThanSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileId is greater than DEFAULT_FILE_ID
        defaultAppFileShouldNotBeFound("fileId.greaterThan=" + DEFAULT_FILE_ID);

        // Get all the appFileList where fileId is greater than SMALLER_FILE_ID
        defaultAppFileShouldBeFound("fileId.greaterThan=" + SMALLER_FILE_ID);
    }


    @Test
    @Transactional
    public void getAllAppFilesByFileNameIsEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileName equals to DEFAULT_FILE_NAME
        defaultAppFileShouldBeFound("fileName.equals=" + DEFAULT_FILE_NAME);

        // Get all the appFileList where fileName equals to UPDATED_FILE_NAME
        defaultAppFileShouldNotBeFound("fileName.equals=" + UPDATED_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileNameIsNotEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileName not equals to DEFAULT_FILE_NAME
        defaultAppFileShouldNotBeFound("fileName.notEquals=" + DEFAULT_FILE_NAME);

        // Get all the appFileList where fileName not equals to UPDATED_FILE_NAME
        defaultAppFileShouldBeFound("fileName.notEquals=" + UPDATED_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileNameIsInShouldWork() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileName in DEFAULT_FILE_NAME or UPDATED_FILE_NAME
        defaultAppFileShouldBeFound("fileName.in=" + DEFAULT_FILE_NAME + "," + UPDATED_FILE_NAME);

        // Get all the appFileList where fileName equals to UPDATED_FILE_NAME
        defaultAppFileShouldNotBeFound("fileName.in=" + UPDATED_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileNameIsNullOrNotNull() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileName is not null
        defaultAppFileShouldBeFound("fileName.specified=true");

        // Get all the appFileList where fileName is null
        defaultAppFileShouldNotBeFound("fileName.specified=false");
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileNameContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileName contains DEFAULT_FILE_NAME
        defaultAppFileShouldBeFound("fileName.contains=" + DEFAULT_FILE_NAME);

        // Get all the appFileList where fileName contains UPDATED_FILE_NAME
        defaultAppFileShouldNotBeFound("fileName.contains=" + UPDATED_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileNameNotContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileName does not contain DEFAULT_FILE_NAME
        defaultAppFileShouldNotBeFound("fileName.doesNotContain=" + DEFAULT_FILE_NAME);

        // Get all the appFileList where fileName does not contain UPDATED_FILE_NAME
        defaultAppFileShouldBeFound("fileName.doesNotContain=" + UPDATED_FILE_NAME);
    }


    @Test
    @Transactional
    public void getAllAppFilesByFileUriIsEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileUri equals to DEFAULT_FILE_URI
        defaultAppFileShouldBeFound("fileUri.equals=" + DEFAULT_FILE_URI);

        // Get all the appFileList where fileUri equals to UPDATED_FILE_URI
        defaultAppFileShouldNotBeFound("fileUri.equals=" + UPDATED_FILE_URI);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileUriIsNotEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileUri not equals to DEFAULT_FILE_URI
        defaultAppFileShouldNotBeFound("fileUri.notEquals=" + DEFAULT_FILE_URI);

        // Get all the appFileList where fileUri not equals to UPDATED_FILE_URI
        defaultAppFileShouldBeFound("fileUri.notEquals=" + UPDATED_FILE_URI);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileUriIsInShouldWork() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileUri in DEFAULT_FILE_URI or UPDATED_FILE_URI
        defaultAppFileShouldBeFound("fileUri.in=" + DEFAULT_FILE_URI + "," + UPDATED_FILE_URI);

        // Get all the appFileList where fileUri equals to UPDATED_FILE_URI
        defaultAppFileShouldNotBeFound("fileUri.in=" + UPDATED_FILE_URI);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileUriIsNullOrNotNull() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileUri is not null
        defaultAppFileShouldBeFound("fileUri.specified=true");

        // Get all the appFileList where fileUri is null
        defaultAppFileShouldNotBeFound("fileUri.specified=false");
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileUriContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileUri contains DEFAULT_FILE_URI
        defaultAppFileShouldBeFound("fileUri.contains=" + DEFAULT_FILE_URI);

        // Get all the appFileList where fileUri contains UPDATED_FILE_URI
        defaultAppFileShouldNotBeFound("fileUri.contains=" + UPDATED_FILE_URI);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFileUriNotContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where fileUri does not contain DEFAULT_FILE_URI
        defaultAppFileShouldNotBeFound("fileUri.doesNotContain=" + DEFAULT_FILE_URI);

        // Get all the appFileList where fileUri does not contain UPDATED_FILE_URI
        defaultAppFileShouldBeFound("fileUri.doesNotContain=" + UPDATED_FILE_URI);
    }


    @Test
    @Transactional
    public void getAllAppFilesByOriginalFileNameIsEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where originalFileName equals to DEFAULT_ORIGINAL_FILE_NAME
        defaultAppFileShouldBeFound("originalFileName.equals=" + DEFAULT_ORIGINAL_FILE_NAME);

        // Get all the appFileList where originalFileName equals to UPDATED_ORIGINAL_FILE_NAME
        defaultAppFileShouldNotBeFound("originalFileName.equals=" + UPDATED_ORIGINAL_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByOriginalFileNameIsNotEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where originalFileName not equals to DEFAULT_ORIGINAL_FILE_NAME
        defaultAppFileShouldNotBeFound("originalFileName.notEquals=" + DEFAULT_ORIGINAL_FILE_NAME);

        // Get all the appFileList where originalFileName not equals to UPDATED_ORIGINAL_FILE_NAME
        defaultAppFileShouldBeFound("originalFileName.notEquals=" + UPDATED_ORIGINAL_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByOriginalFileNameIsInShouldWork() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where originalFileName in DEFAULT_ORIGINAL_FILE_NAME or UPDATED_ORIGINAL_FILE_NAME
        defaultAppFileShouldBeFound("originalFileName.in=" + DEFAULT_ORIGINAL_FILE_NAME + "," + UPDATED_ORIGINAL_FILE_NAME);

        // Get all the appFileList where originalFileName equals to UPDATED_ORIGINAL_FILE_NAME
        defaultAppFileShouldNotBeFound("originalFileName.in=" + UPDATED_ORIGINAL_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByOriginalFileNameIsNullOrNotNull() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where originalFileName is not null
        defaultAppFileShouldBeFound("originalFileName.specified=true");

        // Get all the appFileList where originalFileName is null
        defaultAppFileShouldNotBeFound("originalFileName.specified=false");
    }

    @Test
    @Transactional
    public void getAllAppFilesByOriginalFileNameContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where originalFileName contains DEFAULT_ORIGINAL_FILE_NAME
        defaultAppFileShouldBeFound("originalFileName.contains=" + DEFAULT_ORIGINAL_FILE_NAME);

        // Get all the appFileList where originalFileName contains UPDATED_ORIGINAL_FILE_NAME
        defaultAppFileShouldNotBeFound("originalFileName.contains=" + UPDATED_ORIGINAL_FILE_NAME);
    }

    @Test
    @Transactional
    public void getAllAppFilesByOriginalFileNameNotContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where originalFileName does not contain DEFAULT_ORIGINAL_FILE_NAME
        defaultAppFileShouldNotBeFound("originalFileName.doesNotContain=" + DEFAULT_ORIGINAL_FILE_NAME);

        // Get all the appFileList where originalFileName does not contain UPDATED_ORIGINAL_FILE_NAME
        defaultAppFileShouldBeFound("originalFileName.doesNotContain=" + UPDATED_ORIGINAL_FILE_NAME);
    }


    @Test
    @Transactional
    public void getAllAppFilesByFilePathIsEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where filePath equals to DEFAULT_FILE_PATH
        defaultAppFileShouldBeFound("filePath.equals=" + DEFAULT_FILE_PATH);

        // Get all the appFileList where filePath equals to UPDATED_FILE_PATH
        defaultAppFileShouldNotBeFound("filePath.equals=" + UPDATED_FILE_PATH);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFilePathIsNotEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where filePath not equals to DEFAULT_FILE_PATH
        defaultAppFileShouldNotBeFound("filePath.notEquals=" + DEFAULT_FILE_PATH);

        // Get all the appFileList where filePath not equals to UPDATED_FILE_PATH
        defaultAppFileShouldBeFound("filePath.notEquals=" + UPDATED_FILE_PATH);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFilePathIsInShouldWork() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where filePath in DEFAULT_FILE_PATH or UPDATED_FILE_PATH
        defaultAppFileShouldBeFound("filePath.in=" + DEFAULT_FILE_PATH + "," + UPDATED_FILE_PATH);

        // Get all the appFileList where filePath equals to UPDATED_FILE_PATH
        defaultAppFileShouldNotBeFound("filePath.in=" + UPDATED_FILE_PATH);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFilePathIsNullOrNotNull() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where filePath is not null
        defaultAppFileShouldBeFound("filePath.specified=true");

        // Get all the appFileList where filePath is null
        defaultAppFileShouldNotBeFound("filePath.specified=false");
    }

    @Test
    @Transactional
    public void getAllAppFilesByFilePathContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where filePath contains DEFAULT_FILE_PATH
        defaultAppFileShouldBeFound("filePath.contains=" + DEFAULT_FILE_PATH);

        // Get all the appFileList where filePath contains UPDATED_FILE_PATH
        defaultAppFileShouldNotBeFound("filePath.contains=" + UPDATED_FILE_PATH);
    }

    @Test
    @Transactional
    public void getAllAppFilesByFilePathNotContainsSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where filePath does not contain DEFAULT_FILE_PATH
        defaultAppFileShouldNotBeFound("filePath.doesNotContain=" + DEFAULT_FILE_PATH);

        // Get all the appFileList where filePath does not contain UPDATED_FILE_PATH
        defaultAppFileShouldBeFound("filePath.doesNotContain=" + UPDATED_FILE_PATH);
    }


    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId equals to DEFAULT_USER_ID
        defaultAppFileShouldBeFound("userId.equals=" + DEFAULT_USER_ID);

        // Get all the appFileList where userId equals to UPDATED_USER_ID
        defaultAppFileShouldNotBeFound("userId.equals=" + UPDATED_USER_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsNotEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId not equals to DEFAULT_USER_ID
        defaultAppFileShouldNotBeFound("userId.notEquals=" + DEFAULT_USER_ID);

        // Get all the appFileList where userId not equals to UPDATED_USER_ID
        defaultAppFileShouldBeFound("userId.notEquals=" + UPDATED_USER_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsInShouldWork() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId in DEFAULT_USER_ID or UPDATED_USER_ID
        defaultAppFileShouldBeFound("userId.in=" + DEFAULT_USER_ID + "," + UPDATED_USER_ID);

        // Get all the appFileList where userId equals to UPDATED_USER_ID
        defaultAppFileShouldNotBeFound("userId.in=" + UPDATED_USER_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsNullOrNotNull() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId is not null
        defaultAppFileShouldBeFound("userId.specified=true");

        // Get all the appFileList where userId is null
        defaultAppFileShouldNotBeFound("userId.specified=false");
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId is greater than or equal to DEFAULT_USER_ID
        defaultAppFileShouldBeFound("userId.greaterThanOrEqual=" + DEFAULT_USER_ID);

        // Get all the appFileList where userId is greater than or equal to UPDATED_USER_ID
        defaultAppFileShouldNotBeFound("userId.greaterThanOrEqual=" + UPDATED_USER_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId is less than or equal to DEFAULT_USER_ID
        defaultAppFileShouldBeFound("userId.lessThanOrEqual=" + DEFAULT_USER_ID);

        // Get all the appFileList where userId is less than or equal to SMALLER_USER_ID
        defaultAppFileShouldNotBeFound("userId.lessThanOrEqual=" + SMALLER_USER_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsLessThanSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId is less than DEFAULT_USER_ID
        defaultAppFileShouldNotBeFound("userId.lessThan=" + DEFAULT_USER_ID);

        // Get all the appFileList where userId is less than UPDATED_USER_ID
        defaultAppFileShouldBeFound("userId.lessThan=" + UPDATED_USER_ID);
    }

    @Test
    @Transactional
    public void getAllAppFilesByUserIdIsGreaterThanSomething() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        // Get all the appFileList where userId is greater than DEFAULT_USER_ID
        defaultAppFileShouldNotBeFound("userId.greaterThan=" + DEFAULT_USER_ID);

        // Get all the appFileList where userId is greater than SMALLER_USER_ID
        defaultAppFileShouldBeFound("userId.greaterThan=" + SMALLER_USER_ID);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultAppFileShouldBeFound(String filter) throws Exception {
        restAppFileMockMvc.perform(get("/api/app-files?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$.[*].id").value(hasItem(appFile.getId().intValue())))
                .andExpect(jsonPath("$.[*].fileId").value(hasItem(DEFAULT_FILE_ID.intValue())))
                .andExpect(jsonPath("$.[*].fileName").value(hasItem(DEFAULT_FILE_NAME)))
                .andExpect(jsonPath("$.[*].fileUri").value(hasItem(DEFAULT_FILE_URI)))
                .andExpect(jsonPath("$.[*].originalFileName").value(hasItem(DEFAULT_ORIGINAL_FILE_NAME)))
                .andExpect(jsonPath("$.[*].filePath").value(hasItem(DEFAULT_FILE_PATH)))
                .andExpect(jsonPath("$.[*].userId").value(hasItem(DEFAULT_USER_ID.intValue())));

        // Check, that the count call also returns 1
        restAppFileMockMvc.perform(get("/api/app-files/count?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(content().string("1"));
    }

    /**
     * Executes the search, and checks that the default entity is not returned.
     */
    private void defaultAppFileShouldNotBeFound(String filter) throws Exception {
        restAppFileMockMvc.perform(get("/api/app-files?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(jsonPath("$").isArray())
                .andExpect(jsonPath("$").isEmpty());

        // Check, that the count call also returns 0
        restAppFileMockMvc.perform(get("/api/app-files/count?sort=id,desc&" + filter))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(content().string("0"));
    }

    @Test
    @Transactional
    public void getNonExistingAppFile() throws Exception {
        // Get the appFile
        restAppFileMockMvc.perform(get("/api/app-files/{id}", Long.MAX_VALUE))
                .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateAppFile() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        int databaseSizeBeforeUpdate = appFileRepository.findAll().size();

        // Update the appFile
        AppFile updatedAppFile = appFileRepository.findById(appFile.getId()).get();
        // Disconnect from session so that the updates on updatedAppFile are not directly saved in db
        em.detach(updatedAppFile);
        updatedAppFile
                .fileId(UPDATED_FILE_ID)
                .fileName(UPDATED_FILE_NAME)
                .fileUri(UPDATED_FILE_URI)
                .originalFileName(UPDATED_ORIGINAL_FILE_NAME)
                .filePath(UPDATED_FILE_PATH)
                .userId(UPDATED_USER_ID);
        AppFileDTO appFileDTO = appFileMapper.toDto(updatedAppFile);

        restAppFileMockMvc.perform(put("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isOk());

        // Validate the AppFile in the database
        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeUpdate);
        AppFile testAppFile = appFileList.get(appFileList.size() - 1);
        assertThat(testAppFile.getFileId()).isEqualTo(UPDATED_FILE_ID);
        assertThat(testAppFile.getFileName()).isEqualTo(UPDATED_FILE_NAME);
        assertThat(testAppFile.getFileUri()).isEqualTo(UPDATED_FILE_URI);
        assertThat(testAppFile.getOriginalFileName()).isEqualTo(UPDATED_ORIGINAL_FILE_NAME);
        assertThat(testAppFile.getFilePath()).isEqualTo(UPDATED_FILE_PATH);
        assertThat(testAppFile.getUserId()).isEqualTo(UPDATED_USER_ID);
    }

    @Test
    @Transactional
    public void updateNonExistingAppFile() throws Exception {
        int databaseSizeBeforeUpdate = appFileRepository.findAll().size();

        // Create the AppFile
        AppFileDTO appFileDTO = appFileMapper.toDto(appFile);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restAppFileMockMvc.perform(put("/api/app-files").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(appFileDTO)))
                .andExpect(status().isBadRequest());

        // Validate the AppFile in the database
        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteAppFile() throws Exception {
        // Initialize the database
        appFileRepository.saveAndFlush(appFile);

        int databaseSizeBeforeDelete = appFileRepository.findAll().size();

        // Delete the appFile
        restAppFileMockMvc.perform(delete("/api/app-files/{id}", appFile.getId()).with(csrf())
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<AppFile> appFileList = appFileRepository.findAll();
        assertThat(appFileList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
